package gui.swings;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTextField;

public class SwingMenu extends JPanel {

	static final Boolean bt = new Boolean(true), bf = new Boolean(false);

	static class MType {
		MType(int i) {

		}
	}

	static final MType mi = new MType(1), cb = new MType(2), rb = new MType(3);

	JTextField tf = new JTextField(10);
	JLabel label = new JLabel("Icon selected", SwingFaces.faces[0],
			JLabel.CENTER);

	ActionListener al = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			tf.setText(((JMenuItem) e.getSource()).getText());
		}

	};

	ActionListener al2 = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			JMenuItem jmi = (JMenuItem) e.getSource();
			label.setText(jmi.getText());
			label.setIcon(jmi.getIcon());

		}

	};

	public Object[][] fileMenu = { { "File", new Character('F') },
			{ "New", mi, new Character('N'), al, bt },
			{ "Open", mi, new Character('O'), al, bt },
			{ "Save", mi, new Character('S'), al, bf },
			{ "Save as", mi, new Character('A'), al, bf },
			{ "Exit", mi, new Character('x'), al, bt } };

	public Object[][] editMenu = { { "Edit", new Character('E') },
			{ "Cut", mi, new Character('t'), al, bt },
			{ "Copy", mi, new Character('C'), al, bt },
			{ "Paste", mi, new Character('V'), al, bt }, { null },
			{ "Select All", mi, new Character('l'), al, bt } };

	public Object[] menuBar = { fileMenu, editMenu };

	SwingMenu() {
		this.setLayout(new BorderLayout());

		this.add(createMenuBar(menuBar), BorderLayout.NORTH);
		JPanel p = new JPanel();
		p.setLayout(new BorderLayout());
		p.add(tf, BorderLayout.NORTH);
		p.add(tf, BorderLayout.SOUTH);
		p.add(tf, BorderLayout.CENTER);
		this.add(p, BorderLayout.CENTER);
	}

	private Component createMenuBar(Object[] menuBarData) {
		JMenu menu = new JMenu();
		for (int i = 0; i < menuBarData.length; i++) {
			menu.add(createMenu((Object[][]) menuBarData[i]));
		}
		return menu;
	}

	ButtonGroup bgroup;

	private JMenuItem createMenu(Object[][] menuData) {
		JMenu menu = new JMenu();
		menu.setText((String) menuData[0][0]);
		menu.setMnemonic(((Character) menuData[0][1]).charValue());
		bgroup = new ButtonGroup();
		for (int i = 1; i < menuData.length; i++) {
			if (menuData[i][0] == null) {
				menu.add(new JSeparator());
			} else
				menu.add(createMenuItem(menuData[i]));
		}
		return menu;
	}

	private JMenuItem createMenuItem(Object[] data) {
		JMenuItem jm = null;
		MType type = (MType) data[1];
		if (type == mi) {
			jm = new JMenuItem();
		} else if (type == cb) {
			jm = new JMenuItem();
		} else if (type == rb) {
			jm = new JRadioButtonMenuItem();
			bgroup.add(jm);
		}
		jm.setText((String) data[0]);
		jm.setEnabled(((Boolean) data[4]).booleanValue());
		jm.setMnemonic(((Character) data[2]).charValue());
		jm.addActionListener((ActionListener) data[3]);

		if (data.length == 6) {
			jm.setIcon((Icon) data[5]);
		}
		return jm;
	}

	public static void main(String[] args) {
		SwingShow.inFrame(new SwingMenu(), 300, 200);
	}

}
